<?php

class School_TitleController extends Zend_Controller_Action
{
    private $_store_path ;
    function init()
    {
        $this->_store_path = $this->_helper->getStorePath('school/title_img'); // 取得存放路徑
        $this->view->store_path = $this->_store_path;
    }

    function indexAction()
    {

        $q = Doctrine_Query::create()
        ->from('Teacher_Model_TeacherTitle t')
        ->orderBy('sort');
        $this->view->data = $q->fetchArray();
        $teacherTitle = new Teacher_Model_TeacherTitle();
        $this->view->title_kind = $teacherTitle->getTitleKind();
        if ($this->_request->getParam('act'))
        $this->view->act = $this->_request->getParam('act');

        $room = new School_Model_SchoolRoom();

        $this->view->room_kind = $room->getKind();
    }

    function editAction()
    {
        if ($this->_request->isPost()){
            $room = new School_Model_SchoolRoom();
            $roomKind = $room->getKind();
            $name = $this->_request->getParam('name');
            $sort = $this->_request->getParam('sort');
            $name_short = $this->_request->getParam('name_short');
            $kind_sn = $this->_request->getParam('kind_sn');
            $division_sn = $this->_request->getParam('division_sn');

            $duty = $this->_request->getParam('duty');
            // TODO
            // 更改為 Doctrine_Table
            foreach($name as $sn=>$val) {
                $sn = (int)$sn;
                if ($sn > 0 ) {
                    $t = new Teacher_Model_TeacherTitle();
                    $t->assignIdentifier($sn);
                    $t->name = $val;
                    $t->name_short = $name_short[$sn];
                    $t->sort = $sort[$sn];
                    $t->kind_sn = $kind_sn[$sn];
                    $t->division_sn = $division_sn[$sn];
                    $t->duty = $duty[$sn];
                    $t->enable = 'Y';
                    $t->save();

                    // 修改繼承表
                    if ($division_sn[$sn]) {
                        // 先刪除 非 Register 的繼承表
                        $q = Doctrine_Query::create()
                        ->delete ('Default_Model_AclInheritance')
                        ->where('child_id=?',$val)
                        ->andWHere('parent_id<>?','Register');
                        $q->execute();

                        $parentId  =  $roomKind[$division_sn[$sn]];
                        $q = Doctrine_Query::create()
                        ->from("Default_Model_AclInheritance")
                        ->where('child_id=?',$val)
                        ->andWhere('parent_id=?', $parentId);
                        $res = $q->fetchOne();
                        //                        echo $val.'---'.$parentId;
                        //                        print_r($res);exit;
                        if (!$res) {
                            $acl = new Default_Model_AclInheritance();
                            $acl->parent_id = $parentId;
                            $acl->child_id = $val;
                            $acl->save();
                        }

                        // 重設acl
                        $cache = Zend_Registry::get('cache');
                        $cache->remove('acl_cache');
                    }
                }
            }
        }

        $this->_redirect('/school/title/index');
    }

    function addAction()
    {
        $name = $this->_request->getParam('name');
        if ($this->_request->isPost() and $name <>''){
            $name = $this->_request->getParam('name');
            $sort = $this->_request->getParam('sort');
            $name_short = $this->_request->getParam('name_short');
            $kind_sn = $this->_request->getParam('kind_sn');
            $division_sn = $this->_request->getParam('division_sn');
            $duty = $this->_request->getParam('duty');
            $teacherTitle = new Teacher_Model_TeacherTitle();

            //$row = $teacher_title->createRow();
            $teacherTitle->name= $name;
            $teacherTitle->sort = $sort;
            $teacherTitle->name_short = $name_short;
            $teacherTitle->kind_sn = $kind_sn;
            $teacherTitle->duty  = $duty;
            $teacherTitle->enable = 'Y';
            $teacherTitle->division_sn = $division_sn;
            $teacherTitle->save();

            $room = new School_Model_SchoolRoom();
            $roomKind = $room->getKind();

            $AclInheritance = new Default_Model_AclInheritance(); // 角色繼承
            $AclInheritance->child_id = $name;
            if ($division_sn)
            $AclInheritance->parent_id = $roomKind[$division_sn];
            else
            $AclInheritance->parent_id = 'Register';
            $AclInheritance->replace();
        }
        $this->_redirect('/school/title/index/act/'.$this->view->translate('appendmode'));
    }

    function deleteAction()
    {
        if ($this->_request->isPost()){
            $sn = $this->_request->getParam('sn_hidden',0);
            $teacherTitle  = Doctrine::getTable('Teacher_Model_TeacherTitle')
            ->findOneBy('sn',$sn);
            if ($teacherTitle) {
                $teacherTitle->delete();
            }
        }
        $this->_redirect('/school/title');
    }


    function uploadimgAction()
    {
        if ($this->_request->isPost()) {
            //儲存圖像 /title_img
            if (file_exists($_FILES['title_img']['tmp_name'])){
                $alias = "title_".$this->_request->getParam('img_sn');
                copy($_FILES['title_img']['tmp_name'],$this->_store_path.'/'.$alias);
            }

        }
        $this->_redirect('school/title');
    }

    /**
     * 查看圖檔
     */
    public function getimgAction()
    {

        $name = $this->_getParam('file');
        $this->_helper->downloadFile($name,'school/title_img');
    }

    /**
     * 刪除簽名檔
     *
     * @return unknown
     */

    function delimgAction(){
        $this->getHelper('layout')->disableLayout();
        $this->getHelper('ViewRenderer')->setNorender();
        if ($this->_request->isPost()) {
            $sn = (int) $this->_request->getParam('sn',0);
            if ($sn >0) {
                if (is_file($this->_store_path.'title_'.$sn)){
                    //file_put_contents('/tmp/aa',$this->_store_path.'title_'.$sn);
                    unlink($this->_store_path.'title_'.$sn);
                    echo true;
                }
            }
        }
        echo false;
    }

}